// cQPSelectSubroomsPage.cpp : implementation file
//

#include "stdafx.h"
#include "qpmig.h"
#include "cQPSelectSubroomsPage.h"
#include "cNotesFolderInfo.h"
#include "helper.h"
#include "noteshelperfunctions.h"
#include ".\cqpselectsubroomspage.h"
extern CQPMigApp theApp;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// cQPSelectSubroomsPage property page

IMPLEMENT_DYNCREATE(cQPSelectSubroomsPage, CPropertyPageEx)

cQPSelectSubroomsPage::cQPSelectSubroomsPage() : CPropertyPageEx(cQPSelectSubroomsPage::IDD,0,IDS_QPSUBROOMSHEADERTITLE,IDS_QPSUBROOMSHEADERSUBTITLE)
{
	//{{AFX_DATA_INIT(cQPSelectSubroomsPage)
	m_useCustomSecurity = FALSE;
	m_bSelectAll = FALSE;
	m_bSelectedBranch = FALSE;

	//}}AFX_DATA_INIT
}

cQPSelectSubroomsPage::~cQPSelectSubroomsPage()
{
}

void cQPSelectSubroomsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(cQPSelectSubroomsPage)
	DDX_Control(pDX, IDC_TREE1, m_treeSubRooms);
	DDX_Check(pDX, IDC_CHECK1, m_useCustomSecurity);
	DDX_Check(pDX, IDC_CHECK2, m_bSelectAll);
	DDX_Check(pDX, IDC_CHECK3, m_bSelectedBranch);

	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(cQPSelectSubroomsPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(cQPSelectSubroomsPage)
	// NOTE: the ClassWizard will add message map macros here
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// cQPSelectSubroomsPage message handlers

BOOL cQPSelectSubroomsPage::OnSetActive() 
{
	CPropertySheet* psheet = (CPropertySheet*) GetParent();   
	psheet->SetWizardButtons(PSWIZB_BACK | PSWIZB_NEXT);
	//m_lbItems.ResetContent();
	CString cParent;
	HTREEITEM hParent=TVI_ROOT;
	HTREEITEM hItem=TVI_ROOT;
	m_treeSubRooms.SetMultiSelect(TRUE);

	for(int i=0;i<=theApp.stControl.pdb->cobjNotesFolders.GetUpperBound();i++)
	{
		cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)theApp.stControl.pdb->cobjNotesFolders.GetAt(i);
		CString cFolderName=pFolderInfo->m_strFolderDisplayName;
		cFolderName.MakeLower();
		if(pFolderInfo)
		{
			CString cDisplay=cFolderName;
			if(pFolderInfo->m_wType==SUBROOM)
			{

				if(cFolderName.Left(cParent.GetLength())==cParent && cParent.GetLength()>1)
				{
					cParent=cFolderName.Left(cFolderName.Find("\\",cParent.GetLength())+1);
					hParent=hItem;
					cParent=cFolderName+"\\";

				}
				else if(cFolderName.Find("\\",0)>0) //Find the correct parent;
				{
					HTREEITEM hParentTemp=m_treeSubRooms.GetParentItem(hItem);
					BOOL Found=FALSE;
					while(hParentTemp && !Found)
					{
						TVITEM item;
						item.hItem=hParentTemp;
						item.mask=TVIF_HANDLE;
						if(m_treeSubRooms.GetItem(&item))
						{
							cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)item.lParam;
							CString cTempFolder=pFolderInfo->m_strFolderDisplayName;
							cTempFolder.MakeLower();
							cTempFolder+="\\";
							if(cFolderName.Left(cTempFolder.GetLength())==cTempFolder)
							{
								hParent=hParentTemp;
								cParent=cTempFolder;
								Found=TRUE;;
							}
						}
						hParentTemp=m_treeSubRooms.GetParentItem(hParentTemp);
					}
					if(!Found)
					{
						cParent=cFolderName+"\\";
						hParent=TVI_ROOT;
					}

				}
				else
				{
					cParent=cFolderName+"\\";
					hParent=TVI_ROOT;
				}

				int nPos=cFolderName.ReverseFind('\\');
				if(nPos>0)
					cDisplay=pFolderInfo->m_strFolderDisplayName.Mid(nPos+1);
				hItem=m_treeSubRooms.InsertItem(cDisplay,0,0,hParent);
				BOOL bRet = m_treeSubRooms.SetItemData ( hItem , (DWORD)pFolderInfo) ;
				/*						int id=m_lbItems.AddString(pFolderInfo->m_strFolderDisplayName);
				if(LB_ERR!=id)
				{
				m_lbItems.SetItemDataPtr(id,pFolderInfo);
				}*/
			}


		}

	}

	return CPropertyPageEx::OnSetActive();
}

LRESULT cQPSelectSubroomsPage::OnWizardNext() 
{
	
	UpdateData(TRUE);
	theApp.stControl.m_bSubRoomSecurity=m_useCustomSecurity; 
	DWORD dwErrCode=0;
	CString cTemp;
	if(m_bSelectAll)
	{
		for(int iRoom=0; iRoom<=theApp.stControl.pdb->cobjNotesFolders.GetUpperBound();iRoom++)
		{
			cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)theApp.stControl.pdb->cobjNotesFolders.GetAt(iRoom);
			if(!pFolderInfo)
				continue;
			if(pFolderInfo->m_wType!=SUBROOM)
				continue;
			pFolderInfo->b_Transfer=TRUE;
		}
	}
	else if(m_bSelectedBranch)
	{
		HTREEITEM hItem=m_treeSubRooms.GetFirstSelectedItem();
		HTREEITEM hThisItem=hItem;
		theApp.stControl.pDebugLog->LogOneLine("The following subroom branch will be processed:");
		theApp.stControl.pDebugLog->Flush();
		while(hItem!=NULL)
		{
			HTREEITEM hParent=hItem;
			hParent=m_treeSubRooms.GetParentItem(hItem);
			while(hParent)
			{
				TVITEM item;
				item.hItem=hParent;
				item.mask=TVIF_HANDLE;
				if(m_treeSubRooms.GetItem(&item))
				{
					cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)item.lParam;
					if(pFolderInfo)
					{
						theApp.stControl.pDebugLog->LogOneLine(pFolderInfo->m_strFolderDisplayName);
						theApp.stControl.pDebugLog->Flush();
						pFolderInfo->b_Transfer=TRUE;
					}
				}
				hParent=m_treeSubRooms.GetParentItem(hParent);
			}
			TVITEM item;
			item.hItem=hItem;
			item.mask=TVIF_HANDLE;
			if(m_treeSubRooms.GetItem(&item))
			{
				cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)item.lParam;
				if(pFolderInfo)
				{
					theApp.stControl.pDebugLog->LogOneLine(pFolderInfo->m_strFolderDisplayName);
					theApp.stControl.pDebugLog->Flush();
					pFolderInfo->b_Transfer=TRUE;
				}
			}
			hItem=m_treeSubRooms.GetNextSelectedItem(hItem);
		}
		//Select all children
		hItem=hThisItem;
		if(m_treeSubRooms.ItemHasChildren(hItem))
			EnumerateChildren(m_treeSubRooms.GetChildItem(hItem));

	}
	else
	{
		HTREEITEM hItem=m_treeSubRooms.GetFirstSelectedItem();

		theApp.stControl.pDebugLog->LogOneLine("The following subrooms will be processed:");
		theApp.stControl.pDebugLog->Flush();

		while(hItem!=NULL)
		{

			HTREEITEM hParent=hItem;
			hParent=m_treeSubRooms.GetParentItem(hItem);
			while(hParent)
			{
				TVITEM item;
				item.hItem=hParent;
				item.mask=TVIF_HANDLE;
				if(m_treeSubRooms.GetItem(&item))
				{
					cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)item.lParam;
					if(pFolderInfo)
					{
						theApp.stControl.pDebugLog->LogOneLine(pFolderInfo->m_strFolderDisplayName);
						theApp.stControl.pDebugLog->Flush();
						pFolderInfo->b_Transfer=TRUE;
					}
				}
				hParent=m_treeSubRooms.GetParentItem(hParent);
			}
			TVITEM item;
			item.hItem=hItem;
			item.mask=TVIF_HANDLE;
			if(m_treeSubRooms.GetItem(&item))
			{
				cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)item.lParam;
				if(pFolderInfo)
				{
					theApp.stControl.pDebugLog->LogOneLine(pFolderInfo->m_strFolderDisplayName);
					theApp.stControl.pDebugLog->Flush();
					pFolderInfo->b_Transfer=TRUE;
				}
			}
			hItem=m_treeSubRooms.GetNextSelectedItem(hItem);
		}
	}
	/*	for(int i=0;i<m_lbItems.GetCount();i++)
	{
	if(m_lbItems.GetSel(i))
	{
	cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)m_lbItems.GetItemDataPtr(i);
	if(pFolderInfo)
	{
	pFolderInfo->b_Transfer=TRUE;
	//theApp.stControl.cobjNotesFolders.Add(pFolderInfo);
	/* 						  IXMLDOMElementPtr pLibraryNode=theApp.stControl.pXMLDoc->createElement(L"container");
	IXMLDOMElementPtr pNode=theApp.stControl.pXMLDoc->createElement(L"name");
	BSTR bstr=pFolderInfo->m_strFolderDisplayName.AllocSysString();
	pNode->put_text(bstr);
	theApp.stControl.m_strCurDir=theApp.stControl.m_strDirectoryPath+theApp.stControl.m_strQPName+"\\"+pFolderInfo->m_strFolderDisplayName;
	pLibraryNode->appendChild(pNode);
	pNode.Release();
	::SysFreeString(bstr);
	pNode=theApp.stControl.pXMLDoc->createElement(L"type");
	pNode->put_text(L"Area");
	pLibraryNode->appendChild(pNode);

	pNode=theApp.stControl.pXMLDoc->createElement(L"template");
	pNode->put_text(L"");
	pLibraryNode->appendChild(pNode);
	pNode.Release();



	if(ProcessSubRoom(pFolderInfo,pLibraryNode))
	{
	theApp.stControl.pContainerNode->appendChild(pLibraryNode);
	pLibraryNode.Release();
	}
	//							
	}
	}
	}*/

	return CPropertyPageEx::OnWizardNext();
}

int cQPSelectSubroomsPage::EnumerateChildren(HTREEITEM hItem)
{
	HTREEITEM hNextItem=hItem;
	HTREEITEM hChildItem=hItem;

	while (hChildItem!= NULL)
	{
		TVITEM item;
		item.hItem=hNextItem;
		item.mask=TVIF_HANDLE;
		if(m_treeSubRooms.GetItem(&item))
		{
			cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)item.lParam;
			if(pFolderInfo)
			{
				theApp.stControl.pDebugLog->LogOneLine(pFolderInfo->m_strFolderDisplayName);
				theApp.stControl.pDebugLog->Flush();
				pFolderInfo->b_Transfer=TRUE;
			}
		}

		if(m_treeSubRooms.ItemHasChildren(hNextItem))
			EnumerateChildren(m_treeSubRooms.GetChildItem(hNextItem));

		hNextItem = m_treeSubRooms.GetNextItem(hChildItem, TVGN_NEXT);
		hChildItem=hNextItem;
	}

	return 0;
}
